﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kugar.Core.Authority;
using Kugar.Core.Configuration;
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons;
using Kugar.MVC.MobileUI.WeUI.PageControls.PageForm;

namespace Kugar.MVC.MobileUI.WeUI
{
    public class MobileWeUI
    {
        private static string _resourceUrl = "";
        private static bool _isDebugModel = false;
        private ViewContext _viewContext = null;
        private int _index = 0;
        private PageCommonCtrl _ctrl = null;
        private SystemComponent _systemComponent = null;
        private OtherJsPluginStatus _otherJsPlugin = null;

        static MobileWeUI()
        {
            _resourceUrl = CustomConfigManager.Default.AppSettings.GetValueByName<string>("WebUI:ResourceUrl", "");

            if (string.IsNullOrWhiteSpace(_resourceUrl))
            {
                _resourceUrl = "http://ylfile.b0.upaiyun.com/Inspinia/Content";
            }

            _isDebugModel =
                CustomConfigManager.Default.AppSettings.GetValueByName<string>("WebUI:DebugModel", "false")
                    .ToBool(false);
        }

        internal MobileWeUI(ViewContext viewContext)
        {
            _viewContext = viewContext;

            _viewContext.ViewBag.WebUI = this;

            _ctrl=new PageCommonCtrl(this);

            _systemComponent=new SystemComponent(viewContext);

            _otherJsPlugin=new OtherJsPluginStatus(viewContext);
        }

        public static string LayoutPath => "~/Views/Shared/MobileWeUILayout.cshtml";

        public int NextControlID()
        {
            _index += 1;

            return _index;
        }

        public string Title
        {
            get { return ((string) _viewContext.ViewBag.Title).ToStringEx(); }
            set { _viewContext.ViewBag.Title = value.ToStringEx(); }
        }

        public bool IsDebugModel => _isDebugModel;

        public bool IsInModal
        {
            get { return ((bool?) ViewContext.ViewBag.IsInModal).GetValueOrDefault(false); }
        }

        public bool IsInForm
        {
            get
            {
                var count=(int)ViewContext.TempData.TryGetValue("FormCount", 0);

                return count > 0;
            }
        }

        public PageForm_Form Form()=>new PageForm_Form(_viewContext);

        public AuthorityValidator AuthorityValidator { set; get; } = EmptyAuthorityValidator.Empty;

        public PageCommonCtrl CommonCtrl() => _ctrl;

        public ViewContext ViewContext => _viewContext;

        //public MobileWeUIPage Page => (MobileWeUIPage)ViewContext.ViewBag.MobileWeUIPage;

        /// <summary>
        /// 返回MobileWeUILayout的路径
        /// </summary>
        public string DefaultLayoutPath => "~/Views/Shared/MobileWeUILayout.cshtml";

        public class PageCommonCtrl
        {
            private MobileWeUI _weui = null;

            public PageCommonCtrl(MobileWeUI weui)
            {
                _weui = weui;
            }
            
            protected MobileWeUI WeUI => _weui;

            public PageCommon_Cells Cells()=>new PageCommon_Cells(_weui._viewContext);

            /// <summary>
            /// 输出一个flex控件容器
            /// </summary>
            /// <returns></returns>
            public PageCommon_FlexLayoutContainer FlexLayoutContainer()=>new PageCommon_FlexLayoutContainer(_weui._viewContext);

            /// <summary>
            /// 输出一个Flex控件,,非容器,不需要using
            /// </summary>
            /// <returns></returns>
            public PageCommon_FlexLayout FlexLayout() => new PageCommon_FlexLayout(_weui._viewContext);
            
            /// <summary>
            /// 用于输出九宫格
            /// </summary>
            /// <returns></returns>
            public PageCommon_GridLayout GridLayout()=>new PageCommon_GridLayout(_weui._viewContext);

            //public PageCommon_List List()=>new PageCommon_List(_weui._viewContext);

            /// <summary>
            /// 输出一个loadmore控件,,建议不直接使用该控件,而是使用listview或者tabbar或者navbar等自带的loadmore功能
            /// </summary>
            /// <returns></returns>
            public PageCommon_LoadMore LoadMore()=>new PageCommon_LoadMore(_weui._viewContext);

            /// <summary>
            /// 消息页面,可以使用 WebUIJS.ShowMsgPageByUrl 弹出,也可以直接在页面中输出
            /// </summary>
            /// <returns></returns>
            public PageCommon_MsgPage MsgPage()=>new PageCommon_MsgPage(_weui._viewContext);

            /// <summary>
            /// 输出一个带标题的panel容器控件
            /// </summary>
            /// <returns></returns>
            public PageCommon_Panel Panel()=>new PageCommon_Panel(_weui._viewContext);

            /// <summary>
            /// 图片轮播控件
            /// </summary>
            /// <returns></returns>
            public PageCommon_Swiper Swiper()=>new PageCommon_Swiper(_weui._viewContext);

            /// <summary>
            /// 轮播控件,使用容器的方式输出轮播子项
            /// </summary>
            /// <returns></returns>
            public PageCommon_SwiperContainer SwiperContainer()=>new PageCommon_SwiperContainer(_weui._viewContext);

            /// <summary>
            /// 输出一个TabBar,一般用于首页,按钮在底部
            /// </summary>
            /// <returns></returns>
            public PageCommon_TabBar TabBar()=>new PageCommon_TabBar(_weui._viewContext);

            /// <summary>
            /// 输出导航栏,按钮栏在顶部
            /// </summary>
            /// <returns></returns>
            public PageCommon_NavBar NavBar()=>new PageCommon_NavBar(_weui._viewContext);

            /// <summary>
            /// 输出一个模态弹出框的容器
            /// </summary>
            /// <returns></returns>
            public PageCommon_ModalWindow Modal()=>new PageCommon_ModalWindow(_weui._viewContext);

            /// <summary>
            /// 输出一个listview容器控件,并允许定义每行的模板
            /// </summary>
            /// <typeparam name="TData"></typeparam>
            /// <returns></returns>
            public PageCommon_ListViewContainer<TData> ListView<TData>()=>new PageCommon_ListViewContainer<TData>(_weui._viewContext);

            /// <summary>
            /// 输出一个底部元素
            /// </summary>
            /// <returns></returns>
            public PageCommon_Footer PageFooter()=>new PageCommon_Footer(_weui._viewContext);

            
            public PageCommon_ListActionBar ListActionBar() => new PageCommon_ListActionBar(_weui._viewContext);

            public PageCommon_ListQueryBox ListQueryBox()=>new PageCommon_ListQueryBox(_weui._viewContext);

            /// <summary>
            /// 输出一个footer底部的元素容器
            /// </summary>
            /// <returns></returns>
            public PageCommon_FooterContainer PageFooterContainer()=>new PageCommon_FooterContainer(_weui._viewContext);

            /// <summary>
            /// 数据选择器
            /// </summary>
            /// <typeparam name="TData"></typeparam>
            /// <returns></returns>
            public PageCommon_DataPicker<TData> DataPicker<TData>()=>new PageCommon_DataPicker<TData>(_weui._viewContext);

            public PageCommon_PopupWindow PopupWindow()=>new PageCommon_PopupWindow(_weui._viewContext);
        }

        public SystemComponent SystemComponent() => _systemComponent;

        public OtherJsPluginStatus JsPluginStatus => _otherJsPlugin;
    }

    /// <summary>
    /// 第三方组件使用状态
    /// </summary>
    public class OtherJsPluginStatus
    {
        private ViewContext _viewContext;

        public OtherJsPluginStatus(ViewContext viewContext)
        {
            _viewContext = viewContext;

            //_viewContext.ViewBag.Swiper = false;
            //_viewContext.ViewBag.CityPicker = false;
        }

        public bool Swiper
        {
            get { return ((bool?) _viewContext.ViewBag.Swiper).GetValueOrDefault(false); }
        }

        public bool CityPicker
        {
            get { return ((bool?)_viewContext.ViewBag.CityPicker).GetValueOrDefault(false); }
        }
    }


    public class SystemComponent
    {
        private ViewContext _viewContext = null;

        internal SystemComponent(ViewContext viewContext)
        {
            _viewContext = viewContext;
        }

        /// <summary>
        /// 输出服务器端的弹出框信息,只有在加载弹出页或者自定义页面的时候,才需要使用
        /// </summary>
        /// <returns></returns>
        public PageCommon_ServerMsg ServerMsg() => new PageCommon_ServerMsg(_viewContext);
    }

}